export default {

  dateFormatter(row, column, cellValue) {
    if (cellValue === undefined) return ''

    const d = new Date(cellValue)
    const Y = d.getFullYear()
    const M = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
    const D = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
    const date = Y + '-' + M + '-' + D;
    return date
  },
  formatTimes(row, column, cellValue) {
    if (!cellValue) { return false }
    const d = new Date(cellValue)
    let month = (d.getMonth() + 1).toString();
    let day = d.getDate().toString();
    if (month.length === 1) {
      month = '0' + month;
    }
    if (day.length === 1) {
      day = '0' + day;
    }
    let minutes = d.getMinutes().toString();
    if (minutes.length === 1) {
      minutes = '0' + minutes
    }

    let seconds = d.getSeconds().toString();
    if (seconds.length === 1) {
      seconds = '0' + seconds
    }
    let hours = d.getHours().toString();
    if (hours.length === 1) {
      hours = '0' + hours
    }
    return d.getFullYear() + '-' + month + '-' + day
    // return d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  },
  timeFormatter(cellValue) {
    if (cellValue === undefined) return ''

    const d = new Date(cellValue)
    const Y = d.getFullYear()
    const M = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
    const D = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
    const date = Y + '-' + M + '-' + D;
    return date
  },

  // 排序方法
  sortByKey(array, key, order) {
    return array.sort((a, b) => {
      const x = Number(a[key]) || '0';
      const y = Number(b[key]) || '0';
      const c = a[key] || '0';
      const k = b[key] || '0';
      if (Object.prototype.toString.call(x) === '[object Number]' || Object.prototype.toString.call(y) === '[object Number]') {
        if (order === 'descending') {
          return y - x
        } else {
          return x - y
        }
      } else {
        if (order === 'descending') {
          return k.localeCompare(c)
        } else {
          return c.localeCompare(k)
        }
      }
    })
  },
  /**
 * 深拷贝(可拷贝对象或数组)
 */
  extend(source) {
    const sourceCopy = source.constructor === Array ? [] : {}
    for (const keys in source) {
      if (source.hasOwnProperty(keys)) {
        if (source[keys] && typeof source[keys] === 'object') {
          sourceCopy[keys] = source[keys].constructor === Array ? [] : {}
          sourceCopy[keys] = this.extend(source[keys])
        } else {
          sourceCopy[keys] = source[keys]
        }
      }
    }
    return sourceCopy
  }
}
